package com.edu.framework.websocket.handler;

import com.alibaba.fastjson2.JSONArray;
import com.edu.common.utils.StringUtils;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.info.InfoRequest;
import com.edu.framework.websocket.message.registinfo.RegistInfoRequest;
import com.edu.framework.websocket.service.BaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Map;

@Component
public class RegistInfoRequestHandler implements IHandler<RegistInfoRequest> {

    @Autowired
    private BaseInfoService baseInfoService;

    @Override
    public void execute(WebSocketSession session, RegistInfoRequest request) {
        if (request == null) {
            return;
        }
        Map<String, String> data = request.getData();
        String type = data.get("type");

        String code = WebSocketHolder.getCodeBySession(session);
        Response bindInfo = baseInfoService.getBindInfo(code);
        try {
            WebSocketHolder.send(session,bindInfo);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return RegistInfoRequest.TYPE;
    }
}
